jQuery(document).ready(function() {
	$('#kit-search-more').on('click', function() {
		$('.kit-search-mored').toggle();
	});

	var col = [{
			checkbox: 'checkbox'
		},
        {
            title: '购买ID',
            field: 'id',
            align: 'center',
            valign: 'middle'
        },
		{
			title: '购买日期',
			field: 'purchaseDate',
			align: 'center',
			valign: 'middle'
		},
		{
			title: '学生英文名',
			field: 'studentName',
			align: 'center',
			valign: 'middle',
            formatter: function (value, row, index) {
                return '<a href="javascript:;" style="color: red;" onclick="detail(\''+row.studentId+'\')">'+row.studentName+'</a>';
            }
		},
		{
			title: 'Zoom 账号',
			field: 'zoomId',
			align: 'center',
			valign: 'middle'
		},
		{
			title: '外教',
			field: 'teacherName',
			align: 'center',
			valign: 'middle'
		},
		{
			title: '课程',
			field: 'nameEn',
			align: 'center',
			valign: 'middle'
		},

		{
			//studentName	 
			title: '购买小时',
			//		field:'hoursEnrolled',
			align: 'center',
			valign: 'middle',
			formatter: function(value, row, index) {
				var num = (row.hoursEnrolled / 60).toFixed(2);
				return num;
			}
		},
		{
			title: '销售额',
			field: 'money',
			align: 'center',
			valign: 'middle'
		},
		{
			title: '是否支付',
			field: 'isPay',
			align: 'center',
			valign: 'middle',
		},
		{
			title: '付款方式',
			field: 'payMethod',
			align: 'center',
			valign: 'middle'
		},
		{
			title: '备注',
			field: 'remark',
			align: 'center',
			valign: 'middle'
		},
		{
			title: '课程类型',
			field: 'type',
			align: 'center',
			valign: 'middle'
		},
		{
			title: '编辑',
			formatter: function(value, row, index) {
				var str = '<a href="javascript:;" class="layui-btn layui-btn-sm" onclick="setinfo(\'' + row.uniqueid + '\')">新报</a>' +
					'<a href="javascript:;" class="layui-btn layui-btn-sm" onclick="setinfo(\'' + row.uniqueid + '\')">续报</a>' +
					'<a href="javascript:;" class="layui-btn layui-btn-sm" onclick="reduction(\'' + row.uniqueid + '\')">还原</a>' +
					'<a href="javascript:;" class="layui-btn layui-btn-sm" onclick="curriculumSchedule(\'' + row.uniqueid + '\',\'' + row.teacherId + '\')">考勤</a>';
				return str;
			}
		}
	];

	//------------------------------------------加载列表
	$('#_table').bootstrapTable({
		url:baseUrl+ '/purchaseCourseViews',
		'method': "GET",
		locale: 'zh-CN',
		detailView: false,
		sidePagination: "server", //服务端处理分页
		striped: true, //隔行变色
		undefinedText: "",
		pagination: true,
		singleSelect: false,
		pageSize: pageList[0],
		cache: false, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
		pageNumber: 1,
		pageList: pageList,
		search: false, //不显示 搜索框
		uniqueId: "uniqueid", //每一行的唯一标识，一般为主键列
		showColumns: false, //不显示下拉框（选择显示的列）
		undefinedText: '',
		columns: col,
		queryParams: function(params) {
			return {
				pageNum: this.pageNumber,
				pageSize: this.pageSize,
				sortOrder: params.order
			}
		},

	});

})


var detail = function(uid) {
    parent.layer.open({
        //				title: false,
        maxmin: true,
        type: 2,
        area: modalArea,
        content: './views/form/StudentsDetail.html?studentId='+uid, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
        btn: [ '关闭'],
        btn2: function(index, layero) {
        }
    });
}


var check = function() {

}

function searchByCondition(){
    var studentName = $('#keyword').val();
    if (studentName != '') {
        $("#_table").bootstrapTable('refresh', {url: baseUrl + '/purchaseCourseViews?studentName=' + studentName});
    }
}


var curriculumSchedule = function(uid,teacherId) {
	//uid
	parent.layer.open({
		title: false,
		maxmin: true,
		type: 2,
		area: modalArea,
		content: './views/CurriculumScheduleView.html?uid=' + uid+"&teacherId="+teacherId,
		btn: ['关闭'],
		btn2: function(index, layero) {
			//按钮【按钮二】的回调
			console.log(layero);
			//return false 开启该代码可禁止点击该按钮关闭
		}
	});
}
var doDelete = function() {
	var rows = $('#_table').bootstrapTable('getSelections');
	if(rows == [] || rows == null || rows.length == 0) {
		parent.layer.alert('请选择要编辑的数据！', {
			skin: 'layui-layer-molv', //样式类名
			closeBtn: 0,
			anim: 4,
			time: 2000
		});
		return;
	}
	if(rows.length != 1) {
		parent.layer.alert('请选择单条数据进行编辑！', {
			skin: 'layui-layer-molv', //样式类名
			closeBtn: 0,
			anim: 4,
			time: 1800
		});
		return;
	}
	$.ajax({
		type: "delete",
		url:baseUrl+ '/purchaseCourses/' + rows[0].uniqueid,
		async: true,
		success: function(data) {
			alert(data.msg);
			$('#_table').bootstrapTable('refresh');
		}
	});
}
var setinfo = function(id) {
	parent.layer.open({
		//				title: false,
		maxmin: true,
		type: 2,
		area: modalArea,
		content: './views/form/PurchaseCourseEdit_Plan.html?uid=' + id,
		btn: ['保存', '取消'],
		yes: function(index, layero) {
			var data = $(layero).find("iframe")[0].contentWindow.callback_save();
			console.log(layero);
			$.ajax({
				url:baseUrl+ '/purchaseCourses/addTimes',
				type: 'POST',
				data: data,
				success: function(data) {
					alert(data.msg);
					parent.layer.close(index);
					$('#_table').bootstrapTable('refresh');
				}
			});
		},
		btn2: function(index, layero) {
			//按钮【按钮二】的回调
			console.log(layero);
			//return false 开启该代码可禁止点击该按钮关闭
		}
	});
}
var insert = function() {
	parent.layer.open({
		//				title: false,
		maxmin: true,
		type: 2,
		area: modalArea,
        content: './views/form/PurchaseCourseAdd.html',
		btn: ['保存', '取消'],
		yes: function(index, layero) {
			var data = $(layero).find("iframe")[0].contentWindow.callback_save();
			console.log(layero);
			$.ajax({
                url: baseUrl + '/purchaseCourses',
				type: 'POST',
				data: data,
				success: function(data) {
					alert(data.msg);
					parent.layer.close(index);
					$('#_table').bootstrapTable('refresh');
				}
			});
		},
		btn2: function(index, layero) {
			//按钮【按钮二】的回调
			console.log(layero);
			//return false 开启该代码可禁止点击该按钮关闭
		}
	});
    // parent.layer.open({
     //    //				title: false,
     //    maxmin: true,
     //    type: 2,
     //    area: modalArea,
     //    content: './views/form/PurchaseCourseAdd.html',
     //    btn: ['保存', '取消'],
     //    yes: function(index, layero) {
     //        var data = $(layero).find("iframe")[0].contentWindow.callback_save();
     //        console.log(layero);
     //        $.ajax({
     //            url:baseUrl+ '/admins',
     //            type: 'POST',
     //            data: data,
     //            success: function(data) {
     //                alert(data.msg);
     //                parent.layer.close(index);
     //                $('#_table').bootstrapTable('refresh');
     //            }
     //        });
     //    },
     //    btn2: function(index, layero) {
     //        //按钮【按钮二】的回调
     //        console.log(layero);
     //        //return false 开启该代码可禁止点击该按钮关闭
     //    }
    // });
}

var edit = function() {
	var rows = $('#_table').bootstrapTable('getSelections');
	if(rows == [] || rows == null || rows.length == 0) {
		parent.layer.alert('请选择要编辑的数据！', {
			skin: 'layui-layer-molv', //样式类名
			closeBtn: 0,
			anim: 4,
			time: 2000
		});
		return;
	}
	if(rows.length != 1) {
		parent.layer.alert('请选择单条数据进行编辑！', {
			skin: 'layui-layer-molv', //样式类名
			closeBtn: 0,
			anim: 4,
			time: 1800
		});
		return;
	}
	parent.layer.open({
		//				title: false,
		maxmin: true,
		type: 2,
		area: modalArea,
		// content: './views/form/PurchaseCourseEdit.html?uniqueid=' + rows[0].uniqueid, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
		content: './views/form/PurchaseCourseAdd.html?uniqueid=' + rows[0].uniqueid, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
		btn: ['保存', '取消'],
		yes: function(index, layero) {
			var data = $(layero).find("iframe")[0].contentWindow.callback_save();
			console.log(layero);
			$.ajax({
				url:baseUrl+ '/purchaseCourses/' + rows[0].uniqueid,
				type: 'POST',
				data: data,
				success: function(data) {
					alert(data.msg);
					parent.layer.close(index);
					$('#_table').bootstrapTable('refresh');
				}
			});
		},
		btn2: function(index, layero) {
			//按钮【按钮二】的回调
			alert(index);
			console.log(layero);
			//return false 开启该代码可禁止点击该按钮关闭
		}
	});
}
var reduction = function(id) {
	$.ajax({
		url:baseUrl+ '/purchaseCourses/reduction',
		type: 'POST',
		data: {
			purchaseCourseId: id
		},
		success: function(data) {
			alert(data.msg);
			//			parent.layer.close(index);
			//			$('#_table').bootstrapTable('refresh');
		}
	});
}